/*
 * yNetServer.h
 *
 *  Created on: Sep 13, 2016
 *      Author: root
 */

#ifndef BASE_NET_YNETSERVER_H_
#define BASE_NET_YNETSERVER_H_
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>

#include "yNetBase.h"
#include "yDispatcher.h"

class NetServer {
private:
	static NetServer* instance;
protected:
public:
	int port;
	connection_st g_conn_table[CONN_MAXFD];
	sig_atomic_t shut_server;
	int epfd[EPOLL_NUM];
	int lisSock;
	BaseDispatcher* dispatcher;
	pthread_t worker[NUM_WORKER]; /* echo group has 6 worker threads */
	NetServer(int);
	virtual ~NetServer();
	int start();
	static void shutServerHandler(int signo);
	int sendData(connection_t conn, char *data, int len);
	int handleReadEvent(connection_t conn);
	int handleWriteEvent(connection_t conn);
	void closeConnection(connection_t conn);
	static void *createWorkerThread(void *arg);
	static void *createListenThread(void *arg);
	static NetServer* getInstance();
	virtual void handleMsg(context ctx, char*buf, int len);
};

#endif /* BASE_NET_YNETSERVER_H_ */
